package com.itheima.gatewaymedia.filters;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.itheima.common.constants.SystemConstants;
import com.itheima.common.util.AppJwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
 * @version 1.0
 * @description 网关统一鉴权
 * @package com.itheima.gatewaymedia.filters
 */
@Component
@Slf4j
public class WemediaAuthorizeFilter  implements GlobalFilter, Ordered {




    /**
     * 自媒体登录授权
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //获取请求对象
        ServerHttpRequest req = exchange.getRequest();
        //获取响应对象
        ServerHttpResponse res = exchange.getResponse();
        //白名单放行判断
        //1. 获取请求过来的url地址
        String path = req.getURI().getPath();
        log.debug("请求的地址：{}", path);
        //2. 判断地址是否是后台的登录地址
        if(path.startsWith("/wemedia/login")){
            //是则放行
            return chain.filter(exchange);
        }
        //token校验
        //1. 获取token
        String token = req.getHeaders().getFirst("token");
        //2. 非空判断
        if(StringUtils.isNotEmpty(token)){
            //3. 验证token
            Claims claimsBody = AppJwtUtil.getClaimsBody(token);
            if (null != claimsBody){
                //获取token中的登录id，此时这里自媒体网关，因此得到id为wmUser表中的id
                Object id = claimsBody.get("id");

                //给请求添加当前的用户id，方便下游处理获取
                req.mutate().header(SystemConstants.USER_HEADER_NAME,id.toString());
                    //4. 验证通过则放行
                    return chain.filter(exchange);
                }
        }
        //5. 没有token，或验证没通过的，阻止请求。响应未登录
        res.setStatusCode(HttpStatus.UNAUTHORIZED);
        return res.setComplete();
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
